package com.sunda.spmswms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmswms.entity.Whs;
import com.sunda.spmswms.entity.WhsStorage;
import com.sunda.spmswms.mapper.WerksMapper;
import com.sunda.spmswms.mapper.WhsMapper;
import com.sunda.spmswms.mapper.WhsStorageMapper;
import com.sunda.spmswms.service.IWhsStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 仓库储位信息表 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2021-03-18
 */
@Service
public class WhsStorageServiceImpl extends ServiceImpl<WhsStorageMapper, WhsStorage> implements IWhsStorageService {

    @Autowired
    WhsStorageMapper whsStorageMapper;

    @Autowired
    WerksMapper werksMapper;
    @Autowired
    WhsMapper whsMapper;


    @Override
    public List<WhsStorage> getAllWhsStorageList(List<String> whsCode, List<String> werks) {
        return whsStorageMapper.selectList(new QueryWrapper<WhsStorage>().eq("STORAGE_STATUS","1").in("WERKS", werks).in("WHS_LOCATION_CODE", whsCode));
    }

    /***连带已经删除的储位 */
    @Override
    public List<WhsStorage> getWhsStorageListAll(List<String> whsCode, List<String> werks) {
        return whsStorageMapper.selectList(new QueryWrapper<WhsStorage>().in("WERKS", werks).in("WHS_LOCATION_CODE", whsCode));
    }

    @Override
    public ResponseResult createWhsStorage(WhsStorage whsStorage) {

        // 库区， 库区编号是必选项，如果为空，返回错误信息
        String storageNo=whsStorage.getStorageNo();
        String storageArea=whsStorage.getStorageArea();

        if(null == storageNo || null == storageArea || "".equals(storageNo) || "".equals(storageArea)){
            return ResponseResult.error("验证StorageNo和storageArea失败，所指定的StorageNo和StorageArea不能为空");
        }

        //验证werks和whsLocationCode都存在，如果不存在，侧说明用户输入了非法的数值
        String werks = whsStorage.getWerks();
        String whsLocationCode = whsStorage.getWhsLocationCode();
        int hasWS = whsMapper.selectCount(new QueryWrapper<Whs>().eq("WHS_LOCATION_CODE", whsLocationCode).eq("WERKS", werks));
        if (hasWS <= 0) {
            return ResponseResult.error("验证werks和whsLocationCode失败，所指定的Werks和WHS Location Code组合不存在");
        }

        // 储位状态（默认1启用，0停用）
        String storageStatus=whsStorage.getStorageStatus();
        if (null == storageStatus || "".equals(storageStatus)) {
            storageStatus="1";
        }
        whsStorage.setStorageStatus(storageStatus);

        // uuid 为空则新建数据
        String uuid = whsStorage.getUuid();
        if (null == uuid || "".equals(uuid)) {
            String newUuid=UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
            whsStorage.setUuid(newUuid);
            int result=whsStorageMapper.insert(whsStorage);

            WhsStorage newStorage=whsStorageMapper.selectOne(new QueryWrapper<WhsStorage>().eq("UUID",newUuid));
            return ResponseResult.success().add("whsStorage",newStorage);
        }

        // uuid 存在则更新数据，如果 storageStatus = 0 停用，则更新之前先校验该储位是否有库存，如果有则不允许停用；
        if ("0".equals(storageStatus)){
            List<Map<String, Object>> checkStorageWithInventoryResult = this.baseMapper.checkStorageWithInventory(uuid);
            if (checkStorageWithInventoryResult != null && checkStorageWithInventoryResult.size() > 0){
                return ResponseResult.error("删除前请确保该储位上没有库存信息");
            }
        }

        int result=whsStorageMapper.update(whsStorage,new QueryWrapper<WhsStorage>().eq("UUID",uuid));
        return ResponseResult.success().add("whsStorage",whsStorageMapper.selectOne(new QueryWrapper<WhsStorage>().eq("UUID",uuid)));
    }

    @Override
    public WhsStorage getWhsStorageByUuid(String uuid) {
        return whsStorageMapper.selectOne(new QueryWrapper<WhsStorage>().eq("UUID",uuid));
    }



}
